<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-if="hasPermission('2-3-add')"
          >新增账号</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '修改角色',
                ifShow: hasPermission('2-3-update'),
                onClick: handleEdit.bind(null, record, 'role'),
              },
              {
                label: '修改密码',
                ifShow: hasPermission('2-3-password'),
                onClick: handleEdit.bind(null, record, 'password'),
              },
              {
                color: 'error',
                label: '删除',
                ifShow: hasPermission('2-3-del'),
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup>
  import { usePermission } from '/@/hooks/web/usePermission';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import AccountModal from './AccountModal.vue';
  import { queryaccounts, unregisteraccount } from '/@/api/system/role';
  import { onMounted } from 'vue';
  import { columns } from './account.data';
  import dayjs from 'dayjs';

  const [registerModal, { openModal }] = useModal();
  const { hasPermission } = usePermission();
  const [registerTable, { reload, getPaginationRef }] = useTable({
    title: '账号列表',
    api: queryaccounts,
    columns,
    immediate: false,
    bordered: true,
    showIndexColumn: false,
    actionColumn: hasPermission(['2-3-update', '2-3-password', '2-3-del'])
      ? {
          width: 250,
          title: '操作',
          dataIndex: 'action',
        }
      : undefined,
    fetchSetting: {
      pageField: 'CurrentPage',
      sizeField: 'PageSize',
      totalField: 'TotalCount',
      listField: 'Items',
    },
    beforeFetch: () => {
      return [getPaginationRef().current || 1, getPaginationRef().pageSize || 50];
    },
    afterFetch: (v) => {
      return v.map((item) => {
        item.RegisterTime =
          item.RegisterTime && dayjs(item.RegisterTime).format('YYYY-MM-DD HH:mm:ss');
        return item;
      });
    },
  });

  function handleCreate() {
    openModal(true, {
      type: 'add',
    });
  }

  function handleEdit(record, type) {
    openModal(true, {
      record,
      type,
    });
  }

  async function handleDelete(record) {
    await unregisteraccount([record.UserName]);
    reload();
  }

  function handleSuccess() {
    reload();
  }

  onMounted(() => {
    reload();
  });
</script>
<script lang="ts">
  export default {
    name: 'SystemAccount',
  };
</script>
